# coding=utf-8
# author=haishan09@gmail.com
from django import forms
from cube.models import Provider, Cube
from custom.widgets import JsonifyConfigWidget
from common.configs import configmanager


class ProviderForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(ProviderForm, self).__init__(*args, **kwargs)
        instance = kwargs.get('instance')
        if instance:
            print instance.name
            print configmanager.get_config('cube_provider', instance.name)
            self.fields['config'].widget = JsonifyConfigWidget(configmanager.get_config('cube_provider', instance.name)())
        
    class Meta:
        model = Provider 
        fields = '__all__'


class CubeForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(CubeForm, self).__init__(*args, **kwargs)
        instance = kwargs.get('instance')
        if instance:
            self.fields['config'].widget = JsonifyConfigWidget(configmanager.get_config('cube', instance.provider.name)())
        
    class Meta:
        model = Cube
        fields = '__all__'

